﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;

public partial class RiskItemManage : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["IsAdmin"] != null && Session["IsAdmin"].ToString() == "True")
        {
            InitMenu(); //左边栏
            OutputChapterName();    //右边栏 章节名字+"增加项目"按钮
            SetItemHtml();
        }
        else if (Session["userinfo"] != null && Session["IsAdmin"].ToString() != "True")
        {
            RiskItemManagePage.InnerHtml = "只有管理员才能修改工作项目";
        }
        else
        {
            RiskItemManagePage.InnerHtml = "请先登录！";
        }
    }

    private void InitMenu()
    {
        string menuHTML = "<ul>";
        DBOperate myDB = new DBOperate();
        myDB.SetCommandString("Select * from ChapterList");
        OleDbDataReader dbReader = myDB.ExecuteReader();
        //开始读取数据初始化菜单
        while (dbReader.Read())
        {
            //menuHTML += string.Format("<li>" + dbReader["CNo"] != DBNull.Value ? dbReader["CNo"].ToString() : " ",
            //                        dbReader["CName"].ToString() + "</li>");
            menuHTML += "<li><a href=\"RiskItemManage.aspx?ChapterNo=" + dbReader["No"] + "\">"+ dbReader["CNo"].ToString() + " " + dbReader["CName"].ToString() + "</a></li>";

        }
        menuHTML += "</ul>";
        leftPage.InnerHtml = menuHTML;
        myDB.CloseDB();
    }

    private void OutputChapterName()
    {
        string html = "";
        Int32 No = Convert.ToInt32(Request.QueryString["ChapterNo"]);
        string QueryChapterName = "SELECT * FROM ChapterList WHERE [No] = " + No;
        
        DBOperate MyDb = new DBOperate();
        MyDb.SetCommandString(QueryChapterName);
        OleDbDataReader reader = MyDb.ExecuteReader();
        if (reader.Read()) 
        {
            html += "<input id=\"No\" type=\"hidden\" value=\"" + reader["No"] + "\" /><span>";
            html += reader["CName"];
            html += "</span><span class='button addItem'>增加项目</span>";
        }
        chapterName.InnerHtml = html;
        MyDb.CloseDB();
    }


    private void SetItemHtml()
    {
        string html = "";
        Int32 ChapterNo = Convert.ToInt32(Request.QueryString["ChapterNo"]);

        DBOperate MyDb = new DBOperate();
        string QueryStr = "SELECT * FROM datalist WHERE [ChapterNo] = " + ChapterNo;
        MyDb.SetCommandString(QueryStr);
        OleDbDataReader reader = MyDb.ExecuteReader();
        if (!reader.HasRows)
        {
            html += "没有查找到数据";
        }
        while (reader.Read())
        {
            string measures = reader["Measures"].ToString() == "" ? "无" : reader["Measures"].ToString();
            //string[] Result = reader;
            html += "<div id='item" + reader["No"].ToString() + "' class='itemOuter'>";
            html += "   <div class='item'>";
            html += "       <input class='itemNo' type='hidden' value='" + reader["No"].ToString() +"' />";
            html += "       <span class='itemName'>" + reader["Item"] + "</span><span class='major'>" + reader["Major"] +"</span>";
            html += "   </div>";
            html += "   <div class='faultResult'>";
            html += "       <div class='faultResultTitle title'>可能的差错及后果</div>";
            html += "       <div class='faultResultContent content'>" + EnterEncode(reader["Fault_Result"].ToString()) + "</div>";
            html += "   </div>";
            html += "   <div class='suggestion'>";
            html += "       <div class='suggestionTitle title'>建议采取的措施</div>";
            //html += reader["QC"].ToString()
            html += "       <div class='suggestionContent  content'>";
            html += OutputSpecialSuggestion(reader["Engineer"].ToString(), reader["SkilledPeple"].ToString(), reader["QC"].ToString());
            html += "       </div>";
            html += "   </div>";
            html += "   <div class='specialSuggestion'>";
            html += "       <div class='specialSuggestionTitle title'>专项措施</div>";
            html += "       <div class='specialSuggestionContent  content'>" + EnterEncode(measures) + "</div>";
            html += "   </div>";
            html += "   <div class='manage'><span class='button delete'>删除</span><span class='button modify'>修改</span></div>";
            html += "</div>";
        }
        MyDb.CloseDB();
        rightPageContent.InnerHtml = html;
    }

    private string OutputSpecialSuggestion(string x1, string x2, string x3)
    {
        string html = "";
        if (x1.ToLower() == "true") { html += "<span>*工程师参与</span>"; }
        if (x2.ToLower() == "true") { html += "<span>*熟练人员参与</span>"; }
        if (x3.ToLower() == "true") { html += "<span>*互检</span>"; }
        return html;
    }

    //将回车转换成<br />
    protected string EnterEncode(string str)
    {
        string str1 = Server.UrlEncode(str);
        str1 = str1.Replace("%0a", "%3cbr+%2f%3e");
        str1 = Server.UrlDecode(str1);
        return str1;
    }

}

